package com.ruoyi.web.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.web.domain.BhMeets;
import com.ruoyi.web.domain.BhRiskList;
import com.ruoyi.web.domain.query.AddMeetQuery;
import com.ruoyi.web.domain.query.MeetPageQuery;
import com.ruoyi.web.domain.vo.MeetPageVO;
import com.ruoyi.web.mapper.BhMeetsMapper;
import com.ruoyi.web.mapper.BhRiskListMapper;
import com.ruoyi.web.service.BhMeetsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class BhMeetsServiceImpl implements BhMeetsService {
    @Autowired
    private BhMeetsMapper bhMeetsMapper;
    @Autowired
    private BhRiskListMapper bhRiskListMapper;

    /**
     * 班前会分页查询
     *
     * @param meetPageQuery 查询条件
     * @param pageQuery     分页参数
     * @return 班前会分页数据
     */
    @Override
    public TableDataInfo<MeetPageVO> getMeetPage(MeetPageQuery meetPageQuery, PageQuery pageQuery) {
        LambdaQueryWrapper<BhMeets> lqw = buildQueryWrapper(meetPageQuery);
        Page<MeetPageVO> result = bhMeetsMapper.selectVoPage(pageQuery.build(), lqw);
        List<MeetPageVO> meetPageList = result.getRecords();
        if (CollectionUtils.isNotEmpty(meetPageList)) {
            meetPageList.forEach(x -> {
                fillProjectName(x);
            });
        }
        return TableDataInfo.build(result);
    }

    // 填充作业项目
    private void fillProjectName(MeetPageVO x) {
        List<String> ids = com.ruoyi.common.utils.StringUtils.str2List(x.getBfProjectName(), "[|]", true, true);
        List<BhRiskList> bhRiskLists = bhRiskListMapper.selectList(new LambdaQueryWrapper<BhRiskList>().in(CollectionUtils.isNotEmpty(ids), BhRiskList::getId, ids).orderByAsc(BhRiskList::getId));
        x.setBfProjectName(bhRiskLists.stream().map(BhRiskList::getName).collect(Collectors.joining("|")));
    }

    // 组装分页先查询条件
    private LambdaQueryWrapper<BhMeets> buildQueryWrapper(MeetPageQuery meetPageQuery) {
        LambdaQueryWrapper<BhMeets> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(meetPageQuery.getBfTeamName()), BhMeets::getBfTeamName, meetPageQuery.getBfTeamName());
        lqw.like(StringUtils.isNotBlank(meetPageQuery.getBfWellNo()), BhMeets::getBfWellNo, meetPageQuery.getBfWellNo());
        String date = meetPageQuery.getBfMeetTime();
        if (StringUtils.isNotBlank(date)) {
            DateTime start = DateUtil.beginOfDay(DateUtil.parseDate(date));
            DateTime end = DateUtil.endOfDay(DateUtil.parseDate(date));
            lqw.ge(BhMeets::getBfMeetTime, start);
            lqw.le(BhMeets::getBfMeetTime, end);
        }
        lqw.orderByDesc(BhMeets::getBfMeetTime);
        return lqw;
    }

    /**
     * 新增班前会
     *
     * @param addMeetQuery 新增参数
     * @return 返回
     */
    @Override
    public R<Void> addMeet(AddMeetQuery addMeetQuery) {
        BhMeets addParam = new BhMeets();
        BeanUtils.copyProperties(addMeetQuery, addParam);
        addParam.setDelFlag(0);
        addParam.setCreateTime(new Date());
        addParam.setMeetStep(1);
        Long userId = LoginHelper.getUserId();
        addParam.setCreatedBy(String.valueOf(userId));
        addParam.setBfCommitUser(String.valueOf(userId));
        bhMeetsMapper.insert(addParam);
        return R.ok();
    }

    /**
     * 获取班前会详情
     *
     * @param id 班前会id
     * @return 班前会详情
     */
    @Override
    public R<MeetPageVO> getMeetDetail(Integer id) {
        MeetPageVO meetPageVO = bhMeetsMapper.selectVoById(id);
        fillProjectName(meetPageVO);
        return R.ok(meetPageVO);
    }

    /**
     * 编辑班前会
     *
     * @param addMeetQuery 修改数据
     * @return 返回
     */
    @Override
    public R<Void> modMeet(AddMeetQuery addMeetQuery) {
        BhMeets bhMeets = bhMeetsMapper.selectOne(new LambdaQueryWrapper<BhMeets>().eq(BhMeets::getDelFlag, 0).eq(BhMeets::getId, addMeetQuery.getId()));
        if (Objects.isNull(bhMeets)) {
            return R.fail("未查询到相关会议！");
        }
        Long userId = LoginHelper.getUserId();
        BhMeets modParam = new BhMeets();
        BeanUtils.copyProperties(addMeetQuery, modParam);
        modParam.setUpdatedBy(String.valueOf(userId));
        modParam.setUpdatedTime(new Date());
        modParam.setMeetStep(2);
        return R.ok();
    }
}
